package com.wm.blog_admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.blog_admin.controller.base.AdminBaseController;
import com.wm.blog_admin.controller.req.ButtonRoleReq;
import com.wm.blog_admin.domain.TButtonPermissionDO;
import com.wm.blog_admin.query.TButtonPermissionQuery;
import com.wm.blog_admin.service.TButtonPermissionService;
import com.wm.blog_common.bean.QueryPage;
import com.wm.blog_common.context.AdminBaseContextHandler;
import com.wm.blog_common.entity.TButtonPermission;
import com.wm.blog_common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Optional;


/**
 * <p>
 * 按钮权限表 前端控制器
 * </p>
 *
 * @author 半卷流年
 * @since 2020-05-28
 */
@Slf4j
@RestController
@RequestMapping("/blog/buttonPermission")
public class TButtonPermissionController extends AdminBaseController {

    @Autowired
    private TButtonPermissionService tButtonPermissionService;

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/getDetail")
    public Result<TButtonPermissionDO> getDetail(Long id) {
        return tButtonPermissionService.selectById(id);
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(Long id){
        TButtonPermissionQuery query = new TButtonPermissionQuery();
        query.setId(id);
        return tButtonPermissionService.delete(query);
    }

    /**
     * 添加
     * @param tButtonPermission
     * @return
     */
    @PostMapping
    public Result insert(@RequestBody TButtonPermission tButtonPermission){
        return tButtonPermissionService.insert(tButtonPermission);
    }

    /**
     * 修改
     * @param tButtonPermission
     * @return
     */
    @PutMapping
    public Result update(@RequestBody TButtonPermission tButtonPermission){
        return tButtonPermissionService.update(tButtonPermission);
    }

    /**
     * 分页查询
     * @param current   查询页
     * @param size   每页显示条数
     * @param query 查询参数
     * @return
     */
    @GetMapping("/page")
    public Result<Page<TButtonPermissionDO>> page(Integer current, Integer size, TButtonPermissionQuery query){
        return tButtonPermissionService.page(new QueryPage(current,size),query);
    }


    /**
     * 禁用按钮权限,针对角色设定
     * @return
     */
    @PutMapping("/changeButton")
    public Result changeButton(Integer type,Long id){
        return tButtonPermissionService.changeButton(type,id);
    }

    /**
     * 新增/修改按钮权限
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody TButtonPermissionQuery query){
        query = Optional.ofNullable(query).orElse(new TButtonPermissionQuery());
        return tButtonPermissionService.saveOrUpdate(query);
    }

    /**
     * 角色绑定按钮权限
     * @return
     */
    @PostMapping("/setButtonPermission")
    public Result setButtonPermission(@RequestBody TButtonPermissionQuery query, HttpServletRequest request){
        query = Optional.ofNullable(query).orElse(new TButtonPermissionQuery());
        query.setAdminId(AdminBaseContextHandler.getAdminUserId());
        return tButtonPermissionService.saveOrUpdate(query);
    }


    /**
     * 查询未被禁用的按钮权限
     * @return
     */
    @GetMapping("/buttonList")
    public Result buttonList(TButtonPermissionQuery query){
        query = Optional.ofNullable(query).orElse(new TButtonPermissionQuery());
        return tButtonPermissionService.buttonList(query);
    }

    /**
     * 角色设置按钮权限
     * @return
     */
    @PostMapping("/setButton")
    public Result setButton(@RequestBody TButtonPermissionQuery query,HttpServletRequest request){
        query = Optional.ofNullable(query).orElse(new TButtonPermissionQuery());
        query.setAdminId(AdminBaseContextHandler.getAdminUserId());
        return tButtonPermissionService.setButton(query);
    }

    @PostMapping("/setButtonRole")
    public Result setButtonRole(@Valid @RequestBody ButtonRoleReq req){
        req = Optional.ofNullable(req).orElse(new ButtonRoleReq());
        return tButtonPermissionService.setButtonRole(req);
    }




}
